<?php

namespace App\Func;

use App\Consts\ContractConsts\NewContractApprovalStatus;
use App\Consts\ContractConsts\NewFulfillmentStatusConsts;
use App\Consts\ResponseConsts\ResponseStatusConsts;
use App\Models\NewContract\ContractModel;
use App\Models\NewContract\FulfillmentModel;
use App\Models\NewContract\FulfillmentNodeModel;
use Illuminate\Http\Exceptions\HttpResponseException;

class FulfillmentNodeFuc
{
    /**
     * @param $fulfillmentNodeId
     * @return void
     */
    public static function checkerFulfillmentIsOKByFulfillmentNodeId($fulfillmentNodeId): void
    {
        $fulfillmentNodeModel = new FulfillmentNodeModel();
        $fulfillmentNodeInfo = $fulfillmentNodeModel->getFulfillmentNodeInfo($fulfillmentNodeId,['fulfillment_id']);
        if(!isset($fulfillmentNodeInfo) || !$fulfillmentNodeInfo){
            throw new HttpResponseException(ResponseFunc::returnJson(
                'error',
                '',
                "当前履约节点不存在",
                ResponseStatusConsts::INTERNAL_SERVER_ERROR_500_RESPONSE_STATUS
            ));
        }
        self::checkerFulfillmentIsOKByFulfillmentId($fulfillmentNodeInfo['fulfillment_id']);
    }

    /**
     * @param $fulfillmentId
     * @return void
     */
    public static function checkerFulfillmentIsOKByFulfillmentId($fulfillmentId): void
    {
        $fulfillmentModel = new FulfillmentModel();
        $fulfillmentInfo = $fulfillmentModel->getFulfillmentInfoByFulfillmentId($fulfillmentId,['fulfillment_status','contract_id']);
        if(!isset($fulfillmentInfo) || !$fulfillmentInfo){
            throw new HttpResponseException(ResponseFunc::returnJson(
                'error',
                '',
                "当前履约不存在",
                ResponseStatusConsts::INTERNAL_SERVER_ERROR_500_RESPONSE_STATUS
            ));
        }
        if($fulfillmentInfo['fulfillment_status'] == NewFulfillmentStatusConsts::FULFILLMENT_STATUS_YFQ){
            throw new HttpResponseException(ResponseFunc::returnJson(
                'error',
                '',
                "当前履约已废弃",
                ResponseStatusConsts::INTERNAL_SERVER_ERROR_500_RESPONSE_STATUS
            ));
        }

        $contractModel = new ContractModel();
        $contractInfo = $contractModel->getContractInfo($fulfillmentInfo['contract_id'],"contract_status");
        if(!$contractInfo){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                null,
                "当前合同不存在",
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,
            ));
        }
        // 判断当前合同的状态是否为草稿 如果不为草稿则错误
//        $contractStatus = $contractInfo['contract_status'];
//        if($contractStatus!=NewContractApprovalStatus::CONTRACT_APPROVAL_FWSS && $contractStatus!=NewContractApprovalStatus::CONTRACT_APPROVAL_DSS){
//            throw new HttpResponseException( ResponseFunc::returnJson(
//                'error',
//                null,
//                "当前合同不为草稿不允许新增履约",
//                ResponseStatusConsts::OK_200_RESPONSE_STATUS,
//            ));
//        }

    }


    public static function getFulfillmentResponsibleStaffIdByFulfillmentNodeId($fulfillmentNodeId): int
    {
        $fulfillmentNodeModel = new FulfillmentNodeModel();
        $fulfillmentNodeInfo = $fulfillmentNodeModel->getFulfillmentNodeInfo($fulfillmentNodeId,['fulfillment_id']);
        if(!isset($fulfillmentNodeInfo) || !$fulfillmentNodeInfo){
            throw new HttpResponseException(ResponseFunc::returnJson(
                'error',
                '',
                "当前履约节点不存在",
                ResponseStatusConsts::INTERNAL_SERVER_ERROR_500_RESPONSE_STATUS
            ));
        }
        return self::getFulfillmentResponsibleStaffIdByFulfillmentId($fulfillmentNodeInfo['fulfillment_id']);
    }

    public static function getFulfillmentResponsibleStaffIdByFulfillmentId($fulfillmentId): int
    {
        $fulfillmentModel = new FulfillmentModel();
        $fulfillmentInfo = $fulfillmentModel->getFulfillmentInfoByFulfillmentId($fulfillmentId,['fulfillment_status','contract_id']);
        if(!isset($fulfillmentInfo) || !$fulfillmentInfo){
            throw new HttpResponseException(ResponseFunc::returnJson(
                'error',
                '',
                "当前履约不存在",
                ResponseStatusConsts::INTERNAL_SERVER_ERROR_500_RESPONSE_STATUS
            ));
        }
        if($fulfillmentInfo['fulfillment_status'] == 8){
            throw new HttpResponseException(ResponseFunc::returnJson(
                'error',
                '',
                "当前履约已废弃",
                ResponseStatusConsts::INTERNAL_SERVER_ERROR_500_RESPONSE_STATUS
            ));
        }
        $contractModel = new ContractModel();
        return $contractModel->getContractInfo($fulfillmentInfo['contract_id'],'responsible_staff_id')['responsible_staff_id'];
    }
}